package com.vimi8.chick.plugins.component;

import android.content.Context;
import android.graphics.Color;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.webkit.ConsoleMessage;
import android.webkit.JsResult;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;

import com.alibaba.weex.plugin.annotation.WeexComponent;
import com.eros.framework.extend.comoponents.view.BMWebView;
import com.taobao.weex.WXSDKInstance;
import com.taobao.weex.common.Constants;
import com.taobao.weex.dom.ImmutableDomObject;
import com.taobao.weex.dom.WXDomObject;
import com.taobao.weex.ui.component.WXComponent;
import com.taobao.weex.ui.component.WXComponentProp;
import com.taobao.weex.ui.component.WXVContainer;
import com.taobao.weex.ui.view.IWebView;
import com.taobao.weex.utils.WXUtils;

@WeexComponent(names = {"data-web"})
public class DataWeb extends WXComponent implements IWebView.OnPageListener {
    private BMWebView mWebView;
    private WebView mWeb;
    private String mUrl;

    public DataWeb(WXSDKInstance instance, WXDomObject dom, WXVContainer parent, int type) {
        super(instance, dom, parent, type);
        init();
    }

    public DataWeb(WXSDKInstance instance, WXDomObject dom, WXVContainer parent, String
        instanceId, boolean isLazy) {
        super(instance, dom, parent, instanceId, isLazy);
        init();
    }

    public DataWeb(WXSDKInstance instance, WXDomObject dom, WXVContainer parent, boolean isLazy) {
        super(instance, dom, parent, isLazy);
        init();
    }

    public DataWeb(WXSDKInstance instance, WXDomObject dom, WXVContainer parent) {
        super(instance, dom, parent);
        init();
    }

    private void init() {
        mWebView = new BMWebView(getContext());
    }

    @Override
    protected View initComponentHostView(@NonNull Context context) {
        View view = mWebView.getView();
        mWeb = mWebView.getWebView();

        view.setBackgroundColor(Color.TRANSPARENT);
        WebSettings settings = mWeb.getSettings();
        // settings.setJavaScriptEnabled(true);

        // 自适应屏幕 第一种
        // settings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
        // settings.setLoadWithOverviewMode(true);

        // 自适应屏幕 第二种
        settings.setUseWideViewPort(true);
        settings.setLoadWithOverviewMode(true);

        // 去掉webview的滚动条
        mWeb.setHorizontalScrollBarEnabled(false);//水平不显示
        mWeb.setVerticalScrollBarEnabled(false); //垂直不显示

        mWebView.setOnPageListener(this);
        ImmutableDomObject domObject = getDomObject();
        String url = null;
        if (domObject != null && domObject.getAttrs() != null) {
            url = WXUtils.getString(domObject.getAttrs().get(Constants.Name.SRC), null);
        }
        mUrl = url;
        mWeb.loadDataWithBaseURL(null, url, "text/html", "utf-8", null);
        mWeb.setWebChromeClient(new WebChromeClient() {

            @Override
            public boolean onJsAlert(WebView view, String url, String message, JsResult result) {
                return false;
            }

            @Override
            public boolean onConsoleMessage(ConsoleMessage consoleMessage) {
                String message = consoleMessage.message();
                Log.e("onConsoleMessage", ">>>>>>" + message);
                return super.onConsoleMessage(consoleMessage);
            }
        });

        return view;
    }

    @Override
    public void onReceivedTitle(String title) {

    }

    @Override
    public void onPageStart(String url) {

    }

    @Override
    public void onPageFinish(String url, boolean canGoBack, boolean canGoForward) {

    }

    @WXComponentProp(name = Constants.Name.SRC)
    public void setSrc(String path) {
        if (!TextUtils.isEmpty(path) && !path.equals(mUrl)) {
            mWeb.loadDataWithBaseURL(null, path, "text/html", "utf-8", null);
            mUrl = path;
        }
    }

    @WXComponentProp(name = "zoom")
    public void setZoom(boolean zoom) {
        WebSettings settings = mWeb.getSettings();
        // 设置可以支持缩放
        settings.setSupportZoom(zoom);
        // 设置出现缩放工具
        settings.setBuiltInZoomControls(zoom);
        // 扩大比例的缩放
        settings.setUseWideViewPort(zoom);
    }
}
